package org.sidora.strata.site;

// Test SVN
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import java.util.logging.Level;
import javax.swing.DefaultComboBoxModel;
import org.sidora.core.Site;
import org.sidora.core.util.LoggerProvider;
import org.sidora.strata.Storage;
// Roger

/**
 * SitePanel
 * @author Enric Tartera, Juan Manuel Gimeno, Roger Masgoret
 * @version 1.0
 */
public class SitePanel extends javax.swing.JPanel {
    
    /** Creates new form SitePanel */
    public SitePanel() {
        initComponents();
        fillSiteList();
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jComboBoxSite = new javax.swing.JComboBox();

        jLabel1.setText(java.util.ResourceBundle.getBundle("org/sidora/strata/site/Bundle").getString("Site:"));

        jComboBoxSite.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxSiteItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxSite, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(jComboBoxSite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void jComboBoxSiteItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxSiteItemStateChanged
        refreshSiteExplorer();
    }//GEN-LAST:event_jComboBoxSiteItemStateChanged
    
    private void refreshSiteExplorer(){
        int iSite = jComboBoxSite.getSelectedIndex();
        SiteFormTopComponent win = SiteFormTopComponent.findInstance();
        
        win.setSelectedSite(iSite);
        Storage.setSite((Site) jComboBoxSite.getSelectedItem());
        Storage.getZoneExplorerAction().fillZoneItems();
    }
    
    public void setSelectedSite(int i){
        this.jComboBoxSite.setSelectedIndex(i);
    }
    
    public Site getSelectedSite(){
        if (jComboBoxSite.getSelectedItem() instanceof Site){
            return (Site)jComboBoxSite.getSelectedItem();
        }else{
            return null;
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBoxSite;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
    
    
    public void fillSiteList() {
        DefaultComboBoxModel sitemodel = new DefaultComboBoxModel();
        
        try {
            List list = Site.Inventory.getList();
            TreeSet <Site> sites = new TreeSet<Site>();
            
            Iterator iter2 = list.iterator();
            while(iter2.hasNext()){
                Site site = (Site) iter2.next();
                sites.add(site);
            }
            
            //Ho fiquem en un TreeSet per a tenir la col�lecci� ordenada
            Iterator iter = sites.iterator();
            while (iter.hasNext()){
                sitemodel.addElement(iter.next());
            }
            
        } catch (Exception e1) {
            LoggerProvider.getInstance().log(Level.WARNING,"Exception",e1);
        }
        jComboBoxSite.setModel(sitemodel);
        
        if (sitemodel.getSize()>0)
            refreshSiteExplorer();
        
    }
}
